/*
Copyright 2018 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/

function exportFile(type) {

	var filePath = app.activeDocument.fullName;
	var splitPath = filePath.toString().split('.');
	var originalExtension = splitPath[splitPath.length-1];
	var folderPath = filePath.toString().split('/').slice(0,-1).join('/') + '/';
	var sanitizedFilePath = File(filePath).fsName;

	if (app.name == "Adobe Illustrator") {

		if (type == "pdf") {

			pdfSaveOptions = new PDFSaveOptions();
			pdfSaveOptions.optimization = true;
			app.activeDocument.saveAs(File(sanitizedFilePath), pdfSaveOptions);

		} else if (type == "jpg") {

			var exportOptions = new ExportOptionsJPEG();
			exportOptions.blurAmount = 2;
			exportOptions.matte = true;
			exportOptions.qualitySetting = 100;
			app.activeDocument.exportFile(File(sanitizedFilePath), ExportType.JPEG, exportOptions);

		} else if (type == "png") {

			var exportOptions = new ExportOptionsPNG8();
			exportOptions.matte = true;
			app.activeDocument.exportFile(File(sanitizedFilePath), ExportType.PNG8, exportOptions);

		};

	} else if (app.name == "Adobe Photoshop") {

    if (type == "pdf") {

			var pdfSaveOptions = new PDFSaveOptions();
			pdfSaveOptions.embedColorProfile = true;
			pdfSaveOptions.optimizeForWeb = true;
			app.activeDocument.saveAs(File(sanitizedFilePath), pdfSaveOptions, true);

		} else if (type == "jpg") {

			var jpgSaveOptions = new JPEGSaveOptions();
			jpgSaveOptions.embedColorProfile = true;
			jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
			jpgSaveOptions.matte = MatteType.NONE;
			jpgSaveOptions.quality = 1;
			app.activeDocument.saveAs(File(sanitizedFilePath), jpgSaveOptions, true);

		} else if (type == "png") {

			var pngSaveOptions = new PNGSaveOptions();
			pngSaveOptions.interlaced = false;
			app.activeDocument.saveAs(File(sanitizedFilePath), pngSaveOptions, true);

		} else {

		    var exportOptions = new ExportOptionsSaveForWeb();
		    exportOptions.quality = 60;
		    exportOptions.format = SaveDocumentType.JPEG;
		    exportOptions.includeProfile = true;
	    	app.activeDocument.exportDocument(File(sanitizedFilePath), ExportType.SAVEFORWEB, exportOptions);

		};

	} else if (app.name == "Adobe InDesign") {

		if (type == "pdf") {

			var savePath = sanitizedFilePath.replace(originalExtension,"pdf");
			app.activeDocument.exportFile(ExportFormat.PDF_TYPE, File(savePath), false);

		} else if (type == "jpg") {

			var savePath = sanitizedFilePath.replace(originalExtension,"jpeg");
			app.activeDocument.exportFile(ExportFormat.JPG, File(savePath), false);

		} else if (type == "png") {

			var savePath = sanitizedFilePath.replace(originalExtension,"png");
			app.activeDocument.exportFile(ExportFormat.PNG_FORMAT, File(savePath), false);

		};
	};

	return folderPath;
};
